package im.frape.model;

import im.frape.ContactList;

import java.io.Serializable;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

import android.util.Log;

public class JabberAccount implements Serializable {
	private static final String TAG = "JabberAccount";
	// private FrapeCore core;
	// private FrapeModel model;

	private boolean enabled;

	private String jabberId;
	private String password;
	private String server;
	private Integer port;
	private String resource;
	private boolean encryption;
	private boolean autologin;
	private Presence presence;
	private Roster roster;
	private boolean inFR = false;

	private XMPPConnection connection;

	public JabberAccount(String jabberId, String password) {
		this(jabberId, password, null, null, null, false, false);
	}

	public JabberAccount(String jabberId, String password, String server) {
		this(jabberId, password, server, null, null, false, false);
	}

	public JabberAccount(String jabberId, String password, String server, Integer port, String resource,
			boolean encryption, boolean autologin) {
		this.jabberId = jabberId;
		this.password = password;
		if (server == null) {
			/*
			 * String[] splitedJabberId = jabberId.split("@"); if
			 * (splitedJabberId.length == 2) { this.server = splitedJabberId[1];
			 * }
			 */
		} else {
			this.server = server;
		}
		// this.core = FrapeCore.getInstance();
		// this.model = FrapeModel.getInstance();
		this.port = port;
		this.resource = resource;
		this.encryption = encryption;
		this.autologin = autologin;

	}

	public void connect() {
		ContactList cl = ContactList.getInstance();
		String intServer = null;
		String intJID = null;
		ConnectionConfiguration conf;
		if (server == null) {
			String[] splitedJabberId = jabberId.split("@");
			if (splitedJabberId.length == 2) {
				intJID = splitedJabberId[0];
				intServer = splitedJabberId[1];
			}
		} else {
			intServer = this.server;
			intJID = this.jabberId;
		}
		Log.i(TAG, "Going to connect (" + intJID + ", " + intServer + ")");
		if (connection == null) {
			if (!intServer.equalsIgnoreCase("gmail.com")) {
				conf = new ConnectionConfiguration(intServer);
				/*
				 * Compression isn't possible right now, since it needs smackx.jar
				 * and there is an error when connecting complaining about KeyStore
				 * but it connects...
				 */
				// conf.setCompressionEnabled(true);
				conf.setSASLAuthenticationEnabled(true);
				conf.setSelfSignedCertificateEnabled(true);
				conf.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);

			} else {
				conf = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
				conf.setSASLAuthenticationEnabled(false);
			}
			connection = new XMPPConnection(conf);

		}
		try {
			connection.connect();
			connection.login(intJID, password);
			this.roster = connection.getRoster();
			// roster.addRosterListener(FrapeModel.getInstance());
			// roster.reload();
		} catch (Exception e) {
			Log.e(TAG, "Exception in connect().");
			e.printStackTrace();
		}
	}

	public void disconnect() {
		Log.i(TAG, "Going to disconnect (" + jabberId + ", " + server + ")");
		connection.disconnect();
		this.setInFrapeRoster(false);
	}

	public boolean isConnected() {
		try {
			return connection.isConnected();
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public String toString() {
		return jabberId;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public String getJabberId() {
		return jabberId;
	}

	public String getPassword() {
		return password;
	}

	public String getServer() {
		return server;
	}

	public Integer getPort() {
		return port;
	}

	public String getResource() {
		return resource;
	}

	public boolean isEncryption() {
		return encryption;
	}

	public boolean isAutologin() {
		return autologin;
	}

	public Presence getPresence() {
		return presence;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setJabberId(String jabberId) {
		this.jabberId = jabberId;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}

	public void setEncryption(boolean encryption) {
		this.encryption = encryption;
	}

	public void setAutologin(boolean autologin) {
		this.autologin = autologin;
	}

	public void setPresence(Presence presence) {
		this.presence = presence;
	}

	public Roster getRoster() {
		return this.connection.getRoster();
	}

	public void setInFrapeRoster(boolean bool) {
		this.inFR = bool;
	}

	public boolean inFrapeRoster() {
		return this.inFR;
	}

	public XMPPConnection getConnecton() {
		return this.connection;
	}

}
